#include "groupitemdata.h"
#include <QDebug>

GroupItemData::GroupItemData(QObject *parent) :
    QObject(parent)
{
    m_IsIgnorError = true;
    m_groupNum = "";
}

QString GroupItemData::next()
{
    //judge with start time map.
    //start with second item.
    qDebug()<<"next:"<<m_mapStartTime;
    QString key("");
//    if(m_mapStartTime.count()>1){
////        qDebug()<<"next:"<<m_mapStartTime<<m_startTimeItor.value();
//        QMapIterator<QString,QTime> itor(m_mapStartTime);
//        QString keymin;
//        while(itor.hasNext()){
//            itor.next();
//            QString firstkey = itor.key();
//            if(m_hadPutTb.indexOf(itor.key())!=-1)
//                continue;
//            itor.next();
//            keymin = qMin(firstkey,itor.key());
//        }
//        m_hadPutTb.append(keymin);
//        key = keymin;
//    }
    return key;
}

QString GroupItemData::currentTable()
{
//    if(name.isEmpty()){
//        QMap<QString,QTime>::const_iterator itor = m_mapStartTime.begin();
//        if( itor != m_mapStartTime.constEnd()){
//            return itor.key();
//        }
//    }
//    else
    {
        return m_currentTb;
    }
}

qint32 GroupItemData::getCycleMsecs(int index)
{
    Q_UNUSED(index);
    return m_cycletime.second()*1000 + m_cycletime.minute()*60000 + m_cycletime.hour()*3600000;
}

QTime GroupItemData::getFirstStartTime()
{
    if(!m_mapStartTime.count()){
        return m_starttime;
    }
    QMap<QString,QTime>::const_iterator itor = m_mapStartTime.begin();
    if( itor != m_mapStartTime.constEnd()){
        return itor.value();
    }
}

QTime GroupItemData::getFirstCycleTime()
{
    if(!m_mapCycleTime.count()){
        return m_cycletime;
    }
    QMap<QString,QTime>::const_iterator itor = m_mapCycleTime.begin();
    if( itor != m_mapCycleTime.constEnd()){
        return itor.value();
    }
}

int GroupItemData::getFirstCount()
{
    if(!m_mapCount.count()){
        return m_count;
    }
    QMap<QString,int>::const_iterator itor = m_mapCount.begin();
    if( itor != m_mapCount.constEnd()){
        return itor.value();
    }
}

qint32 GroupItemData::getFirstCycleMsecs()
{
    return getFirstCycleTime().second()*1000 + getFirstCycleTime().minute()*60000 + getFirstCycleTime().hour()*3600000;
}

QTime GroupItemData::getTBStartTime(const QString &tbname)
{
    QMap<QString,QTime>::const_iterator itor = m_mapStartTime.constFind(tbname);
    if( itor != m_mapStartTime.constEnd()){
        return itor.value();
    }
}

QTime GroupItemData::getTBCycleTime(const QString &tbname)
{
    QMap<QString,QTime>::const_iterator itor = m_mapCycleTime.constFind(tbname);
    if( itor != m_mapCycleTime.constEnd()){
        return itor.value();
    }
}

int GroupItemData::getTBCount(const QString &tbname)
{
    QMap<QString,int>::const_iterator itor = m_mapCount.constFind(tbname);
    if( itor != m_mapCount.constEnd()){
        return itor.value();
    }
}

qint32 GroupItemData::getTBCycleMsecs(const QString &tbname)
{
    return getTBCycleTime(tbname).second()*1000 + getTBCycleTime(tbname).minute()*60000 + getTBCycleTime(tbname).hour()*3600000;
}

QTableWidget *GroupItemData::getTableWidget(QString tbName)
{
    QTableWidget *tblwnd = NULL;
    QMap<QString,QTableWidget*>::const_iterator itor = m_MapTblWnds.constFind(tbName);
    if( itor != m_MapTblWnds.constEnd()){
        tblwnd = itor.value();
    }
    return tblwnd;
}

void GroupItemData::insertTBStartTime(const QString &tbname, const QTime &time)
{
    m_mapStartTime.insert(tbname,time);
}

void GroupItemData::insertTBCycleTime(const QString &tbname, const QTime &time)
{
    m_mapCycleTime.insert(tbname,time);
}

void GroupItemData::insertTBCount(const QString &tbname, const int &count)
{
    m_mapCount.insert(tbname,count);
}

void GroupItemData::removeItem(QString key)
{
    if(m_mapStartTime.find(key)!=m_mapStartTime.end()){
        m_mapStartTime.remove(key);
    }
    if(m_mapCycleTime.find(key)!=m_mapCycleTime.end()){
        m_mapCycleTime.remove(key);
    }
    if(m_mapCount.find(key)!=m_mapCount.end()){
        m_mapCount.remove(key);
    }
}

void GroupItemData::setTableWidget(QString tblname, QTableWidget *tblwnd)
{
    m_MapTblWnds.insert(tblname,tblwnd);
}

void GroupItemData::copy(const GroupItemData &other)
{
    this->m_itemText = other.m_itemText;
    this->m_Ipadd = other.m_Ipadd;
    this->m_itemIndex = other.m_itemIndex;

    this->m_groupNum = other.m_groupNum;

    this->m_filedb = other.m_filedb;
    this->m_fileexl = other.m_fileexl;
    this->m_starttime = other.m_starttime;
    this->m_cycletime = other.m_cycletime;
    this->m_count = other.m_count;
    this->m_IsEnable = other.m_IsEnable;
    this->m_IsIgnorError = other.m_IsIgnorError;
    this->m_hadPutTb = other.m_hadPutTb;
    this->m_mapStartTime = other.m_mapStartTime;
    this->m_mapCycleTime = other.m_mapCycleTime;
    this->m_mapCount = other.m_mapCount;
    this->m_currentTb = other.m_currentTb;

    this->m_MapTblWnds = other.m_MapTblWnds;
}
